package Database;

import Entity.Branch;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.Types;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author CONG
 */
public class BranchDB {

    private ConnectDB conn;

    public BranchDB() {
        conn = new ConnectDB();
    }

    public ArrayList<Branch> getBranchList() {
        ArrayList<Branch> listBr = new ArrayList<Branch>();
        try {
            CallableStatement cstmt = conn.connect().prepareCall("{call getBranchList}");
            ResultSet rs = cstmt.executeQuery();
            while (rs.next()) {
                int id = rs.getInt(1);
                String name = rs.getString(2);
                String phone = rs.getString(4);
                String address = rs.getString(3);
                String email = rs.getString(5);
                boolean status = rs.getBoolean(6);
                Branch br = new Branch(id, name, address, phone, email, status);
                listBr.add(br);
            }
        } catch (Exception ex) {
            Logger.getLogger(BranchDB.class.getName()).log(Level.SEVERE, null, ex);
        }
        return listBr;
    }

    public Branch insertBranch(Branch br) {
        try {
            CallableStatement cstmt = conn.connect().prepareCall("{call insertBranch(?,?,?,?,?,?)}");
            cstmt.registerOutParameter("BranchCode", Types.INTEGER);
            cstmt.setString("Name", br.getName());
            cstmt.setString("Address", br.getAddress());
            cstmt.setString("Phone", br.getPhone());
            cstmt.setString("Email", br.getEmail());
            cstmt.setBoolean("Status", br.isStatus());
            cstmt.executeUpdate();
            int branchCode = cstmt.getInt("BranchCode");
            br.setBranchCode(branchCode);
        } catch (Exception ex) {
            Logger.getLogger(BranchDB.class.getName()).log(Level.SEVERE, null, ex);
        }
        return br;
    }

    public void updateBranch(Branch br) {
        try {
            CallableStatement cstmt = conn.connect().prepareCall("{call updateBranch(?,?,?,?,?,?)}");
            cstmt.setInt("BranchCode", br.getBranchCode());
            cstmt.setString("Name", br.getName());
            cstmt.setString("Address", br.getAddress());
            cstmt.setString("Phone", br.getPhone());
            cstmt.setString("Email", br.getEmail());
            cstmt.setBoolean("Status", br.isStatus());
            cstmt.executeUpdate();
        } catch (Exception ex) {
            Logger.getLogger(BranchDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void deleteBranch(Branch br) {
        try {
            CallableStatement cstmt = conn.connect().prepareCall("{call deleteBranch(?)}");
            cstmt.setInt("BranchCode", br.getBranchCode());
            cstmt.executeUpdate();
        } catch (Exception ex) {
            Logger.getLogger(BranchDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public boolean checkEmail(String email){
        try {
            CallableStatement cstmt=conn.connect().prepareCall("{call checkEmail(?)}");
            cstmt.setString("email", email);
            ResultSet rs=cstmt.executeQuery();
            return rs.next();
        } catch (Exception ex) {
            Logger.getLogger(BranchDB.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }
}
